package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐，同时添加菜品信息
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String>save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.saveSetmeal(setmealDto);
        return R.success("cc");
    }
    /**
     * 分页查询，同时查询出套餐分类
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize, String name){

        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        queryWrapper.eq(Setmeal::getIsDeleted,0);
        setmealService.page(setmealPage,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(setmealPage,setmealDtoPage);

        List<Setmeal> records = setmealPage.getRecords();

        ArrayList<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            //new一个SetmealDto用来封装对象
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(record,setmealDto);
            //获取套餐ID
            Long categoryId = record.getCategoryId();
            //根据ID查询套餐对象
            Category byId = categoryService.getById(categoryId);
            //获取套餐名称
            String name1 = byId.getName();
            //重新赋值给setmealDto中的CategoryName
            setmealDto.setCategoryName(name1);
            //添加到SetmealDto集合中
            list.add(setmealDto);
        }
        //重新设置setRecords集合
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    /**
     * 根据ID删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String>delete(String ids){
        log.info("ids={}",ids);
        String[] split = ids.split(",");
        for (String s : split) {
            Setmeal byId = setmealService.getById(Long.parseLong(s));
            if (byId.getIsDeleted()!=1){
                byId.setIsDeleted(1);
            }
            setmealService.updateById(byId);
        }
        return R.success("删除成功");
    }

    /**
     * 根据ID回显数据
     */
    @GetMapping("/{id}")
    public R<SetmealDto>getId(@PathVariable Long id){
        log.info("id={}",id);
        SetmealDto id1 = setmealService.getId(id);
        return R.success(id1);

    }

    /**
     * 修改套餐，同时修改菜品属性
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.updateId(setmealDto);
        return R.success("成功");
    }
    /**
     * 批量停售或者起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String>status(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("status={},ids={}",status,ids);
        for (Long id : ids) {
            log.info(id.toString());
            Setmeal byId = setmealService.getById(id);
            if (byId!=null&status!=null){
                byId.setStatus(status);
                setmealService.updateById(byId);
                R.success("修改成功");
            }
        }

//        Setmeal byId = setmealService.getById(ids);
//        byId.setStatus(status);
//        setmealService.updateById(byId);
        return R.success("修改失败");
    }

    @GetMapping("/list")
    public R<List<Setmeal>>listR(Setmeal setmeal){
        log.info(setmeal.toString());
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getCreateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
